<?php
/**
 * Created by PhpStorm.
 * User: function
 * Date: 2018-04-16
 * Time: 9:14
 */

namespace app\seller\controller;

use app\common\controller\Seller;

use app\common\model\CoType as CoTypeModel;
use app\common\model\Goods;
use app\common\logic\Logic;
use think\Request;
class CoType extends Seller
{

    protected $authenticate=null;
    
    public function index()
    {

       
        if(Request::instance()->isAjax()){
            $logic = new Logic();
            $logic->setValue('modelClass','app\common\model\CoType');

            $logic->setValue('size',20);
            $logic->setValue('paginate',1);
//            $logic->setValue('page',paramForm('page'));
            $logic->setSql(['co_id'=>SID]);

            $list = $logic->prepareDataProvider();
                foreach($list as $cotype){
                $cotype['mallCount'] = Goods::where(['user_id'=>$cotype->co_id,'co_type_id'=>$cotype->id])->count();
                }
            success($list);
        }else{
            return $this->fetch();
        }
        
    }
    

    
    public function add(){
        return $this->fetch();

    }
    
    /**
     * @return string|void
     */
    public function save(){
        
        $rule=[
            'co_id|商户id'=>'require',
            'title|商店分类名称'=>'require'
        ];
        
        $params = paramForm();
        $params['co_id'] = $this->getSellerId();
        $this->validate($params,$rule);

        $cotype = new CoTypeModel();
        $rt = $cotype->allowField(true)->save($params);

        if($rt){
            $this->success('新增成功');
        }else{
            $this->error('新增失败');
        }
        
        
    }
    
    public function edit($id){
        $cotype = CoTypeModel::get($id);
        $this->assign('model',$cotype);
        return $this->fetch();
    }

    public function update($id){
        $cotype = CoTypeModel::get($id);
        $cotype->title = paramForm('title',true);
        $rt = $cotype->isUpdate()->save();
        if($rt){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }

    }

    public function delete($id){
        $cotype = CoTypeModel::get($id);
        $cotype->delete();
        $this->success('删除成功');
    }


    
}